package custom.base.http;


import android.content.Context;
import android.text.TextUtils;

import custom.base.data.Global;
import custom.base.log.MLog;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by luozi on 2015/12/29.
 * baseRequest include requestString and requestBaseResponse and requestList
 */
public class BaseRequest<T> extends HttpProxy {
//    private RequestQueue mQueue = null;


    public BaseRequest(Context context) {
        super(context);
//        mQueue = Volley.newRequestQueue(context);

    }


    //=============================================请求辅助方法==============================


    public String EntityData = "data";
    public String EntityCode = "code";
    public String EntityMsg = "msg";


    /**
     * 把json转换成基础响应对象类
     *
     * @param jsonObject jsonobject对象
     * @param classOfT   待转换的实体类,为空则data为空
     */
//    public final BaseResponse praseBaseResponse(JSONObject jsonObject, Class<T> classOfT) throws JSONException {
//        Object data = null;
//        if (classOfT != null) {
//            if (classOfT == String.class)
//                data = jsonObject.optString(EntityData);
//            else {
//                if (jsonObject.opt(EntityData) != null)
//                    try {
//                        data = JSON.parseObject(jsonObject.optString(EntityData), classOfT);
//                    } catch (Exception e) {
//
//                    }
//
//            }
//        }
//
//        BaseResponse baseResponse = new BaseResponse().
//                setCode(jsonObject.optString(EntityCode))
//                .setMsg(jsonObject.optString(EntityMsg))
//                .setData(data);
//
//        return baseResponse;
//    }

    /**
     * 把json转换成基础响应对象列表类
     */
//    public final BaseResponse praseBaseResponseList(JSONObject jsonObject, Class<T> classOfT) throws JSONException {
//        BaseResponse baseResponse = new BaseResponse().
//                setCode(jsonObject.optString(EntityCode))
//                .setMsg(jsonObject.optString(EntityMsg));
//
//        List<T> list = new ArrayList<>();
//        if (jsonObject.opt(EntityData) != null) {
//            JSONArray jsonArray = jsonObject.optJSONArray(EntityData);
//            if (jsonArray != null)
//                for (int i = 0; i < jsonArray.length(); i++) {
//                    try {
//                        list.add(JSON.parseObject(jsonArray.get(i).toString(), classOfT));
//                    } catch (Exception e) {
//                    }
//                }
//        }
//        baseResponse.setData(list);
//        return baseResponse;
//    }

//    /**
//     * 打印request日志
//     *
//     * @param metoh  请求方式
//     * @param params 请求携带的参数
//     */
//    private final void printfRequestLog(String moduleName, String metohName, Method metoh, RequestParams params) {
//        if (params != null) {
////            MLog.d(Global.getInstance().getAPP_DEBUG_HEADER() + "#" + HttpTag, metoh.toString() + " Request url:" + appendUrl(moduleName, metohName)
////                    + " parms = " + (params.getStringsParams() == null ? "" : params.getStringsParams().toString())
////                    + (params.getFilesParams() == null ? "" : "----  fileParms = " + params.getFilesParams().toString()));
//        } else {
//            MLog.d(Global.getInstance().getAPP_DEBUG_HEADER() + "#" + HttpTag, " Request url:" + getAPPUrl());
//        }
//    }

    /**
     * 打印request日志
     *
     * @param task 任务名
     */
    private final void printfRequestLog(Tasks task, String url) {
        if (url != null) {
            MLog.d(Global.getInstance().getAPP_DEBUG_HEADER() + "#" + HttpTag, task + " Request url:" + getAPPUrl() + " url = " + url.toString());
        } else {
            MLog.d(Global.getInstance().getAPP_DEBUG_HEADER() + "#" + HttpTag, " Request url:" + getAPPUrl());
        }
    }

    /**
     * 打印reponse success日志
     * <p>
     *
     * @param task 任务名
     * @param o    响应结果
     */
    private final void printfReponseLog(Tasks task, Object o) {
        MLog.d(Global.getInstance().getAPP_DEBUG_HEADER() + "#" + HttpTag, task + " Reponse Success = " + (o == null ? NULLTag : o.toString()));
    }

    /**
     * 打印reponse error日志
     *
     * @param task 任务名
     * @param o    响应结果
     */
    private final void printfErrorLog(Tasks task, Object o) {
        MLog.e(Global.getInstance().getAPP_DEBUG_HEADER() + "#" + HttpTag, task + " Reponse Error = " + (o == null ? NULLTag : o.toString()));
    }

    /**
     * 打印reponse success日志
     *
     * @param task 任务名
     */
    private final void printfCancelLog(Tasks task) {
        MLog.d(Global.getInstance().getAPP_DEBUG_HEADER() + "#" + HttpTag, "Reponse Cancel = " +
                (task == null ? NULLTag : task));
    }


    /**
     * Map<String, Object> 泛型转换为 Map<String, String>
     *
     * @param map
     */
    private final static Map<String, String> ObjectToStringMap(Map<String, Object> map) {
        if (map != null) {
            Map<String, String> tmp = new HashMap<>();
            for (int i = 0; i < map.size(); i++) {
                tmp.put(map.keySet().toArray()[i].toString(), map.values()
                        .toArray()[i].toString());
            }
            return tmp;
        }
        return null;
    }

    /**
     * 连接方法名
     */
    private final String appendUrl(String moduleName, String metohName) {
        StringBuilder url = new StringBuilder(getAPPUrl() + (moduleName == null ? "" : moduleName) + (TextUtils.isEmpty(moduleName) ? "" : "/") + metohName + "?");
        return url.toString();
    }


}
